package com.waitingsnow.modules.shell.view.controls
{
import assets.shellView.AlertAsset;

import fl.controls.Button;

import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextFieldAutoSize;

/**
 * 
 * @author waitingsnow
 * $Id: Alert.as 2 2012-05-17 10:10:03Z cgslendy@gmail.com $
 */
public class Alert extends Sprite
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>Alert</code> instance.
	 * 
	 */
	public function Alert(contentStr:String = "", tipStr:String = "提示")
	{
		this.contentStr = contentStr;
		this.tipStr = tipStr;
		initDisplay();
	}
	
	//==========================================================================
	//  Variables
	//==========================================================================
	private var alertAsset:AlertAsset;
	private var contentStr:String;
	private var tipStr:String;
	
	private var okBtn:Button;
	private var cancelBtn:Button;
	
	//==========================================================================
	//  Properties
	//==========================================================================
	public function set okLabel(value:String):void
	{
		okBtn && (okBtn.label = value);
	}

	public function set cancelLabel(value:String):void
	{
		cancelBtn && (cancelBtn.label = value);
	}
	
	//==========================================================================
	//  Methods
	//==========================================================================
	private function initDisplay():void
	{
		alertAsset = new AlertAsset();
		alertAsset.tip_txt.autoSize = TextFieldAutoSize.LEFT;
		alertAsset.content_txt.autoSize = TextFieldAutoSize.LEFT;
		alertAsset.content_txt.multiline = true;
		alertAsset.tip_txt.text = tipStr;
		alertAsset.content_txt.text = contentStr;
		alertAsset.content_txt.width = Math.min(200, alertAsset.content_txt.width);
		alertAsset.content_txt.wordWrap = true;
		alertAsset.content_txt.x = (alertAsset.width - alertAsset.content_txt.width) * 0.5;
		
		var sp:Sprite = new Sprite();
		okBtn = new Button();
		okBtn.label = "确定";
		okBtn.width = 50;
		alertAsset.addChild(okBtn);
		cancelBtn = new Button();
		cancelBtn.label = "取消";
		cancelBtn.width = 50;
		sp.addChild(okBtn);
		cancelBtn.x = okBtn.width + 10;
		sp.addChild(cancelBtn);
		sp.x = (alertAsset.width - 120) * 0.5;
		sp.y = alertAsset.height - 25;
		alertAsset.addChild(sp);
		okBtn.addEventListener(MouseEvent.CLICK, okBtnClickHandler);
		cancelBtn.addEventListener(MouseEvent.CLICK, cancelBtnClickHandler);
		alertAsset.close_btn.addEventListener(MouseEvent.CLICK, cancelBtnClickHandler);
		addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
		addChild(alertAsset);
	}
	
	public function dispose():void
	{
		removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
		parent && parent.removeChild(this);
		okBtn.removeEventListener(MouseEvent.CLICK, okBtnClickHandler);
		cancelBtn.removeEventListener(MouseEvent.CLICK, cancelBtnClickHandler);
		alertAsset.close_btn.removeEventListener(MouseEvent.CLICK, cancelBtnClickHandler);
	}
	
	//==========================================================================
	//  Event Handlers
	//==========================================================================
	private function okBtnClickHandler(event:MouseEvent):void
	{
		dispatchEvent(new Event(Event.COMPLETE));
		dispose();
	}
	
	private function cancelBtnClickHandler(event:MouseEvent):void
	{
		dispatchEvent(new Event(Event.CANCEL));
		dispose();
	}
	
	private function addedToStageHandler(event:Event):void
	{
		x = (stage.stageWidth - width) * 0.5;
		y = (stage.stageHeight - height) * 0.5;
	}
}
}